package com.wu.http.server;


import com.wu.javax.servlet.Servlet;
import com.wu.javax.servlet.ServletContext;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * 单例模式
 * servlet容器
 */
public class MyServletContext implements ServletContext {
    private final Map<String, Servlet> servlets = new Hashtable<>();
    private final Map<String, Object> attributes = new HashMap<>();
    private static MyServletContext myServletContext;

    private MyServletContext() {
    }

    public synchronized static MyServletContext getInstance() {
        if (myServletContext == null) {
            myServletContext = new MyServletContext();
        }
        return myServletContext;
    }

    @Override
    public Map<String, Servlet> getServlets() {
        return this.servlets;
    }

    @Override
    public Servlet getServlet(String servletName) {
        return this.servlets.get(servletName);
    }

    @Override
    public void setServlet(String key, Servlet servlet) {
        this.servlets.put(key, servlet);
    }

    @Override
    public void setAttribute(String key, Object obj) {
        this.attributes.put(key, obj);
    }

    @Override
    public Object getAttribute(String key) {
        return this.attributes.get(key);
    }

}
